package Socket;


import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;//套接字

    public Client() {
        try {
            System.out.println("正在尝试与服务器建立连接....");
//            socket = new Socket("176.212.26.140",8088);
            socket = new Socket("127.0.0.1",8088);
            System.out.println("与服务器建立连接！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start() {
        Scanner scan = new Scanner(System.in);
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);
            String line;
            while(true) {
                line = scan.nextLine();
                if ("exit".equals(line)) {
                    break;
                }
                pw.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();//调用socket.close(),会调用socket获取的输入输出流的close()方法
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();//实例化就在尝试连接
        client.start();
    }
}
